package cn.com.customer.manager.ui.order.addOrder

import cn.com.customer.manager.bean.*
import cn.com.customer.manager.http.BaseApi
import cn.com.customer.manager.http.subscriber.NoTipRequestSubscriber
import cn.com.customer.manager.http.subscriber.TipRequestSubscriber
import cn.com.customer.manager.manager.TokenManager
import com.xuexiang.xhttp2.XHttp
import com.xuexiang.xhttp2.XHttpProxy
import com.xuexiang.xhttp2.annotation.ThreadType
import com.xuexiang.xhttp2.model.ApiResult

class AddOrderPresenter constructor(view: AddOrderContract.View) : AddOrderContract.Presenter {
    var mView: AddOrderContract.View = view

    init {
    }
    override fun addOrder(params: AddOrderInfo) {
        val request = XHttp.custom()
        request.call(request.create(BaseApi.IOrder::class.java)
                .orderAdd(RequestBean(params)))
                .subscribeWith(object : TipRequestSubscriber<ApiResult<Map<String, String>>?>() {
                    override fun onSuccess(apiResult: ApiResult<Map<String, String>>?) {
                        mView.addSuccess()
                    }
                })
    }
    override fun getData(params: MutableMap<String, String>) {
        val request = XHttp.custom()
        request.call(request.create(BaseApi.IOrder::class.java)
                .orderDetail(RequestBean(params)))
                .subscribeWith(object : TipRequestSubscriber<ApiResult<OrderDetailInfo>?>() {
                    override fun onSuccess(apiResult: ApiResult<OrderDetailInfo>?) {
                        apiResult?.let {
                            if (it != null) {
                                mView.setData(it.data)
                            }
                        }
                    }
                })
    }
    override fun getStateList() {
        XHttpProxy.proxy(BaseApi.IOrder::class.java, ThreadType.TO_IO)
                .orderStatusList(TokenManager.getInstance().token)
                .subscribeWith(object : NoTipRequestSubscriber<StatusList>() {
                    override fun onSuccess(t: StatusList?) {
                        t?.let {
                            mView.setStateList(it)
                        }
                    }
                })
    }

    override fun getPayWayList() {
        XHttpProxy.proxy(BaseApi.IOrder::class.java, ThreadType.TO_IO)
                .orderPayWayList(TokenManager.getInstance().token)
                .subscribeWith(object : NoTipRequestSubscriber<PayWayResult>() {
                    override fun onSuccess(t: PayWayResult?) {
                        t?.let {
                            mView.setPayWayList(it)
                        }
                    }
                })

    }
    override fun getGoodList(params: MutableMap<String, String>) {
        val request = XHttp.custom()
        params["pageNo"] = "1"
        params["pageSize"] = "1000"
        request.call(request.create(BaseApi.IOrder::class.java)
                .orderGoodsList(RequestBean(params)))
                .subscribeWith(object : TipRequestSubscriber<ApiResult<GoodListInfo>?>() {
                    override fun onSuccess(apiResult: ApiResult<GoodListInfo>?) {
                        apiResult?.let {
                            if (it != null) {
                                mView.initGoodList(it.data)
                            }
                        }
                    }
                })
    }

    override fun getGoodList() {
        val request = XHttp.custom()
        var params:MutableMap<String,String> = mutableMapOf()
        params["pageNo"] = "1"
        params["pageSize"] = "1000"
        request.call(request.create(BaseApi.IOrder::class.java)
                .goodsList(RequestBean(params)))
                .subscribeWith(object : TipRequestSubscriber<ApiResult<GoodsList>?>() {
                    override fun onSuccess(apiResult: ApiResult<GoodsList>?) {
                        apiResult?.let {
                            mView.setGoodList(it.data)
                        }
                    }
                })

    }

    override fun modifyOder(params: AddOrderInfo) {
        val request = XHttp.custom()
        request.call(request.create(BaseApi.IOrder::class.java)
                .orderModify(RequestBean(params)))
                .subscribeWith(object : TipRequestSubscriber<ApiResult<Map<String, String>>?>() {
                    override fun onSuccess(apiResult: ApiResult<Map<String, String>>?) {
                        mView.modifySuccess()
                    }
                })
    }



    override fun start() {
        getGoodList()
        getPayWayList()
        getStateList()
    }

}